
/**
 * 获取文件后缀名
 * @param {string} fileName [必须] 文件名
 */
export const fileSuffix = (fileName) => {
    let pos = fileName.lastIndexOf('.'),
        suffix = ''
    if (pos !== -1) {
        suffix = fileName.substring(pos)
    }
    return suffix
}
/**
 * 获取文件类型图标
 * @param {string} fileName [必须] 文件名/地址
 */
export const fileIco = (fileName) => {
    let suffix = fileSuffix(fileName), // 得到后缀
        ico = ''
    switch (suffix) {
        case '.doc':
        case '.docx':
            ico = 'word'
            break
        case '.ppt':
        case '.pptx':
            ico = 'ppt'
            break
        case '.xls':
        case '.xlsx':
            ico = 'excel'
            break
        case '.pdf':
            ico = 'pdf'
            break
        case '.jpg':
        case '.jpeg':
        case '.png':
            ico = 'img'
            break
        case '.zip':
        case '.rar':
            ico = 'zip'
            break
        case '.mp4':
            ico = 'video'
            break
        case '.mp3':
            ico = 'audio'
            break
        default:
            ico = 'default'
            break
    }
    return ico
}

/**
 * 格式化历史记录数据
 */
export const classifyMessages = (data) => {
    // 获取当前日期
    const now = new Date();
    // 计算当天的起始时刻
    const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate());
    const yesterday = new Date(now);
    yesterday.setDate(now.getDate() - 1);
    const threeDaysAgo = new Date(now);
    threeDaysAgo.setDate(now.getDate() - 3);
    const sevenDaysAgo = new Date(now);
    sevenDaysAgo.setDate(now.getDate() - 7);
    const thirtyDaysAgo = new Date(now);
    thirtyDaysAgo.setDate(now.getDate() - 30);

    // 初始化结果对象
    const result = {
        "今天": [],
        "昨天": [],
        "3天内": [],
        "7天内": [],
        "30天内": []
    };

    // 遍历数据并分类
    data.forEach(item => {
        const createTime = new Date(item.create_time);
        const content = { id: item.session_id, name: item.content, knowledge_id: item.knowledge_id };

        if (createTime >= todayStart) {
            result["今天"].push(content);
        } else if (createTime >= yesterday) {
            result["昨天"].push(content);
        } else if (createTime >= threeDaysAgo) {
            result["3天内"].push(content);
        } else if (createTime >= sevenDaysAgo) {
            result["7天内"].push(content);
        } else if (createTime >= thirtyDaysAgo) {
            result["30天内"].push(content);
        }
    });

    // 转换为最终格式
    const finalResult = [];
    for (const [title, children] of Object.entries(result)) {
        if (children.length > 0) {
            finalResult.push({
                title: title,
                children: children
            });
        }
    }

    return finalResult;
}

// 导出一个函数，用于从URL中获取指定参数的值
export const getFromUrl = (page) => {
    const currentUrl = window.location.href;
    const url = new URL(currentUrl);
    return url.searchParams.get(page);
};

// 模拟a标签点击下载
export const downloadOnClick = (url, fileName = new Date().getTime()) => {
    const a = document.createElement('a')
    document.body.appendChild(a)
    a.href = url
    a.download = fileName
    a.click()
    window.URL.revokeObjectURL(url)
    document.body.removeChild(a)
}